﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;

namespace SMS.Common.DataAccess
{
    public class DataContextBase : DataContext
    {
        private static string m_ConnStr = null;
        private static MappingSource mappingSource = new AttributeMappingSource();

        public DataContextBase() :
            base(string.Empty, mappingSource)
        {
            if (!string.IsNullOrEmpty(ConnectionString))
            {
                this.Connection.ConnectionString = ConnectionString;
            }
        }

        public DataContextBase(string connection) :
            base(connection, mappingSource)
        {
            if (!string.IsNullOrEmpty(ConnectionString))
            {
                this.Connection.ConnectionString = ConnectionString;
            }
        }

        public DataContextBase(System.Data.IDbConnection connection) :
            base(connection, mappingSource)
        {
            if (!string.IsNullOrEmpty(ConnectionString))
            {
                this.Connection.ConnectionString = ConnectionString;
            }
        }

        public DataContextBase(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
            base(connection, mappingSource)
        {
            if (!string.IsNullOrEmpty(ConnectionString))
            {
                this.Connection.ConnectionString = ConnectionString;
            }
        }

        public DataContextBase(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
            base(connection, mappingSource)
        {
            if (!string.IsNullOrEmpty(ConnectionString))
            {
                this.Connection.ConnectionString = ConnectionString;
            }
        }

        private string m_DatabaseName;
        /// <summary>
        /// 数据库名称
        /// </summary>
        public virtual string DatabaseName
        {
            get
            {
                if (string.IsNullOrEmpty(m_DatabaseName))
                {
                    m_DatabaseName = this.Mapping.DatabaseName;
                }
                return m_DatabaseName;
            }
            set
            {
                m_DatabaseName = value;
            }
        }

        private string m_ConnectionString;
        /// <summary>
        /// 获取或设置默认连接字符串
        /// </summary>
        public virtual string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(m_ConnectionString))
                {
                    m_ConnectionString = ConfigurationManager.AppSettings["ConnStr"];
                }
                var strCatalog = "Initial Catalog";
                if (!string.IsNullOrEmpty(m_ConnectionString) && !m_ConnectionString.Contains(strCatalog) && !string.IsNullOrEmpty(DatabaseName))
                {
                    m_ConnectionString += string.Format(";{0}={1}", strCatalog, DatabaseName);
                }
                return m_ConnectionString;
            }
            set
            {
                m_ConnectionString = value;
            }
        }
    }
}
